package com.Touch
{
	import com.Touch.tuio.TUIOEvent;
	import com.Touch.tuio.TUIOManager;
	import com.Touch.tuio.TUIOObject;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	

	/**
	 * 多点触摸管理类，当需要多点触摸时将此类初始化 
	 * 并传入触摸受体 
	 */
	public class MultTouchManager
	{
		
		/*                           */
		public static const MOUSE_DRAG:int=-1;
		
		
		/*                           */
		
		private var obj:DisplayObject;//执行的对象
		
		private var touch_mode:int;//触摸模式
		
		private var line:Rectangle;//拖动的范围

		private var touchpoint:Vector.<MultTouchPoint>;//触摸点集合
		
		private var tuiopoint:Vector.<TUIOObject>;//TUIO触摸点集合
		
		private const TO_ROT:Number =57.29577951308232;//57.295779513;//角度弧度转换值  180/Math.PI
			
		private var outstop:Boolean;//超出区域停止
		
		public function MultTouchManager(_obj:DisplayObject)
		{
			//初始化
			touchpoint=new Vector.<MultTouchPoint>();
			tuiopoint=new Vector.<TUIOObject>();
			touch_mode=0;
			obj=_obj;
		}
		
		/**
		 * 触摸模式，控制触摸方法
		 * 管理是否可以进行多类型触摸
		 * 0  全功能
		 * -1 鼠标拖拽
		 */
		public function setTouchMode(_mode:int):void{
			touch_mode=_mode;
			//执行
			if(obj.stage==null){
				obj.addEventListener(Event.ADDED_TO_STAGE,on_addtostage);
			}else{
				on_addtostage(null);
			}
		}
		
		/**
		 * 拖动区域
		 */
		public function setRectangle(rect:Rectangle,outStop:Boolean=true):void{
			line=rect;
			outstop=outStop;
		}
		
		/**
		 * 移除触摸
		 */
		public function removeTouch():void{
			obj.removeEventListener(MouseEvent.MOUSE_DOWN,on_mouse_down);
			obj.removeEventListener(MouseEvent.MOUSE_OUT,on_mouse_up);
			obj.removeEventListener(TouchEvent.TOUCH_BEGIN, addtouchpoint);
			obj.stage.removeEventListener(TouchEvent.TOUCH_MOVE, movetouchpoint);
			obj.stage.removeEventListener(TouchEvent.TOUCH_END,removetouchpoint);
			obj.removeEventListener(Event.ENTER_FRAME,dormMultTouch);
			obj=null;
		}
		
		
		//当加入舞台后
		private function on_addtostage(event:Event):void{
			if(touch_mode<0){
				obj.addEventListener(MouseEvent.MOUSE_DOWN,on_mouse_down);
			}else{
				obj.addEventListener(TouchEvent.TOUCH_BEGIN, addtouchpoint);
				obj.stage.addEventListener(TouchEvent.TOUCH_MOVE, movetouchpoint);
				obj.stage.addEventListener(TouchEvent.TOUCH_END,removetouchpoint);
				obj.addEventListener(Event.ENTER_FRAME,dormMultTouch);
			}
		}
		
		//鼠标拖拽
		private function on_mouse_down(event:MouseEvent):void{
			(obj as Object).startDrag(false,line);
			obj.addEventListener(MouseEvent.MOUSE_UP,on_mouse_up);
			if(outstop){
				obj.addEventListener(MouseEvent.MOUSE_OUT,on_mouse_up);
			}
		}
		
		//停止拖拽
		private function on_mouse_up(event:MouseEvent):void{
			obj.removeEventListener(MouseEvent.MOUSE_UP,on_mouse_up);
			obj.removeEventListener(MouseEvent.MOUSE_OUT,on_mouse_up);
			(obj as Object).stopDrag();
		}
		
		
		//新增触摸点
		private function addtouchpoint(event:TouchEvent):void{
			for each(var temp:MultTouchPoint in touchpoint){
				if(temp.id==event.touchPointID){
					temp.updata(event.stageX,event.stageY);
					return
				}
			}
			touchpoint.push(new MultTouchPoint(event.touchPointID,event.stageX,event.stageY));
		}
		
		//移除触摸点
		private function removetouchpoint(event:TouchEvent):void{
			for(var i:int=0;i<touchpoint.length;i++){
				if (touchpoint[i].id==event.touchPointID){
					touchpoint.splice(i,1);
					return;
				}
			}
		}
		
		//更新触摸点的位置
		private function movetouchpoint(event:TouchEvent):void{
			for each(var temp:MultTouchPoint in touchpoint){
				if(temp.id==event.touchPointID){
					temp.updata(event.stageX,event.stageY);;
					return
				}
			}
		}
		
		//缩放、移动图片
		private function dormMultTouch(event:Event):void{
			if(touchpoint.length<1){
				return;
			}
			if(touchpoint.length==1){
				
				obj.x+=touchpoint[0].x-touchpoint[0].oldx;
				obj.y+=touchpoint[0].y-touchpoint[0].oldy;
				
			}else if(touchpoint.length==2){
				
				//获取旧点与新点
				var ntp_0:Point=new Point(touchpoint[0].x,touchpoint[0].y);
				var ntp_1:Point=new Point(touchpoint[1].x,touchpoint[1].y);
				var otp_0:Point=new Point(touchpoint[0].oldx,touchpoint[0].oldy);
				var otp_1:Point=new Point(touchpoint[1].oldx,touchpoint[1].oldy);
				
				//转换中心点位移
				var pointa:Point=Point.interpolate(ntp_0,ntp_1, 0.5);
				var pointb:Point=Point.interpolate(otp_0,otp_1, 0.5);
				
				var sca:Number=Point.distance(ntp_0,ntp_1)/Point.distance(otp_0,otp_1);
				
				var rota:Number=getAngle(ntp_0,ntp_1)-getAngle(otp_0,otp_1);//getAngleTrig(otp_1.y-otp_0.y,otp_1.x-otp_0.x)-getAngleTrig(ntp_1.y-ntp_0.y,ntp_1.x-ntp_0.x);//
				
				if(sca>5){
					sca=5;
				}else if(sca<0.2){
					sca=0.2;
				}
				
				//执行处理
				obj.rotation+=rota;
				
				obj.scaleX=obj.scaleY*=sca;
				
				obj.x+=pointa.x-pointb.x;
				obj.y+=pointa.y-pointb.y;
			}
			for each(var temp:MultTouchPoint in touchpoint){
				temp.handle();
			}
		}
		
		//角度值计算
		private function getAngle(p1:Point,p2:Point):Number{
			var ang:Point=p2.subtract(p1);
			var rt:Number = Math.atan2(ang.y,ang.x);
			rt = rt*TO_ROT;
			return rt;
		}
		
		//旧版本 角度计算
		private function getAngleTrig(X:Number, Y:Number): Number
		{
			if (X == 0.0)
			{
				if(Y < 0.0)
					return 270;
				else
					return 90;
			} else if (Y == 0)
			{
				if(X < 0)
					return 180;
				else
					return 0;
			}
			
			if ( Y > 0.0)
				if (X > 0.0)
					return Math.atan(Y/X) * TO_ROT;
				else
					return 180.0-Math.atan(Y/-X) * TO_ROT;
				else
					if (X > 0.0)
						return 360.0-Math.atan(-Y/X) * TO_ROT;
					else
						return 180.0+Math.atan(-Y/-X) * TO_ROT;
		} 
	}
}